package isg3.data;

import isg3.domain.Categoria;
import isg3.domain.Evento;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class JDBCCategoriaDAO implements ICategoriaDAO {

	private Connection conn;
	
	public JDBCCategoriaDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
	
	public List selectAllCategorias() {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List searchResults = new LinkedList();
        String sql = "SELECT * FROM categorias ";

        try {
            stmt = conn.prepareStatement(sql);
            result = stmt.executeQuery();

            while(result.next()){
            	Categoria cat=new Categoria();
            	cat.setIdCategoria(result.getString("OIDCategoria"));
            	cat.setNombre(result.getString("nombre"));
            	searchResults.add(cat);
            }
            
            
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return searchResults;
    }

}
